# 导入数据库模块
import pymysql
# 导入Flask框架，这个框架可以快捷地实现了一个WSGI应用
from flask import Flask, flash
# 默认情况下，flask在程序文件夹中的templates子文件夹中寻找模块
from flask import render_template
# 导入前台请求的request模块
from flask import request, Blueprint, session
import traceback

shen_ZPFB = Blueprint('shen_zhaopinfabu', __name__)

# 连接数据库
def sql_conn():
    conn = pymysql.connect(
        host='139.217.107.173',
        port=3306,
        user='root',
        password='123456',
        database='project_training',
        charset='utf8'
    )
    cur = conn.cursor()
    # 返回conn和cursor
    return conn, cur

# 关闭连接
def sql_close(conn, cur):
    conn.close()
    cur.close()

@shen_ZPFB.route('/shen_zhaopinfabu', methods=['GET', 'POST'])
def shen_zhaopinfabu():
    u = session.get('user')
    return render_template('zhaopinfabu.html',u=u)

@shen_ZPFB.route('/shen_zhaopinfabu_submit',methods=['GET', 'POST'])
def shen_zhaopinfabu_submit():
    conn, cur = sql_conn()
    id = request.values.get('id')
    zhaopingangwei = request.values.get('zhaopingangwei')
    zhaopinrenshu = request.values.get('zhaopinrenshu')
    email = request.values.get('email')
    beizhu = request.values.get('beizhu')
    # sql语句 增加数据
    sql = "INSERT INTO zhaopin(id,zhaopingangwei,zhaopinrenshu,beizhu,email) value('%s','%s','%s','%s','%s')" % (id,zhaopingangwei,zhaopinrenshu,beizhu,email)
    cur.execute(sql)
    conn.commit()
    sql_close(conn,cur)
    return render_template('zhaopinxinxi.html')

